home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / fl_cursor.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  5.8 KB  |  172 lines

  1. //
  2. // "$Id: fl_cursor.cxx,v 1.6 1999/01/07 19:17:37 mike Exp $"
  3. //
  4. // Mouse cursor support for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Change the current cursor.
  27. // Under X the cursor is attached to the X window.  I tried to hide
  28. // this and pretend that changing the cursor is a drawing function.
  29. // This avoids a field in the Fl_Window, and I suspect is more
  30. // portable to other systems.
  31.  
  32. #include <FL/Fl.H>
  33. #include <FL/Fl_Window.H>
  34. #include <FL/x.H>
  35. #ifndef WIN32
  36. #include <X11/cursorfont.h>
  37. #endif
  38. #include <FL/fl_draw.H>
  39.  
  40. void fl_cursor(Fl_Cursor c, Fl_Color fg, Fl_Color bg) {
  41.   if (Fl::first_window()) Fl::first_window()->cursor(c,fg,bg);
  42. }
  43.  
  44. #ifdef WIN32
  45.  
  46. void Fl_Window::cursor(Fl_Cursor c, Fl_Color, Fl_Color) {
  47.   if (!shown()) return;
  48.   if (c > FL_CURSOR_NESW) {
  49.     i->cursor = 0;
  50.   } else if (c == FL_CURSOR_DEFAULT) {
  51.     i->cursor = fl_default_cursor;
  52.   } else {
  53.     LPSTR n;
  54.     switch (c) {
  55.     case FL_CURSOR_ARROW:    n = IDC_ARROW; break;
  56.     case FL_CURSOR_CROSS:    n = IDC_CROSS; break;
  57.     case FL_CURSOR_WAIT:    n = IDC_WAIT; break;
  58.     case FL_CURSOR_INSERT:    n = IDC_IBEAM; break;
  59.     case FL_CURSOR_HELP:    n = IDC_HELP; break;
  60.     case FL_CURSOR_HAND:    n = IDC_UPARROW; break;
  61.     case FL_CURSOR_MOVE:    n = IDC_SIZEALL; break;
  62.     case FL_CURSOR_N:
  63.     case FL_CURSOR_S:
  64.     case FL_CURSOR_NS:        n = IDC_SIZENS; break;
  65.     case FL_CURSOR_NE:
  66.     case FL_CURSOR_SW:
  67.     case FL_CURSOR_NESW:    n = IDC_SIZENESW; break;
  68.     case FL_CURSOR_E:
  69.     case FL_CURSOR_W:
  70.     case FL_CURSOR_WE:        n = IDC_SIZEWE; break;
  71.     case FL_CURSOR_SE:
  72.     case FL_CURSOR_NW:
  73.     case FL_CURSOR_NWSE:    n = IDC_SIZENWSE; break;
  74.     default:            n = IDC_NO; break;
  75.     }
  76.     i->cursor = LoadCursor(NULL, n);
  77.   }
  78.   SetCursor(i->cursor);
  79. }
  80.  
  81. #else
  82.  
  83. // I like the MSWindows resize cursors, so I duplicate them here:
  84.  
  85. #define CURSORSIZE 16
  86. #define HOTXY 7
  87. struct TableEntry {
  88.   uchar bits[CURSORSIZE*CURSORSIZE/8];
  89.   uchar mask[CURSORSIZE*CURSORSIZE/8];
  90.   Cursor cursor;
  91. } table[] = {
  92.   {{    // FL_CURSOR_NS
  93.    0x00, 0x00, 0x80, 0x01, 0xc0, 0x03, 0xe0, 0x07, 0x80, 0x01, 0x80, 0x01,
  94.    0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
  95.    0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01, 0x00, 0x00},
  96.    {
  97.    0x80, 0x01, 0xc0, 0x03, 0xe0, 0x07, 0xf0, 0x0f, 0xf0, 0x0f, 0xc0, 0x03,
  98.    0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, 0xf0, 0x0f,
  99.    0xf0, 0x0f, 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01}},
  100.   {{    // FL_CURSOR_EW
  101.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10,
  102.    0x0c, 0x30, 0xfe, 0x7f, 0xfe, 0x7f, 0x0c, 0x30, 0x08, 0x10, 0x00, 0x00,
  103.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  104.    {
  105.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x1c, 0x38,
  106.    0xfe, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0x1c, 0x38, 0x18, 0x18,
  107.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
  108.   {{    // FL_CURSOR_NWSE
  109.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x38, 0x00, 0x78, 0x00,
  110.    0xe8, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x17, 0x00, 0x1e, 0x00, 0x1c,
  111.    0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  112.    {
  113.    0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0xfc, 0x00, 0x7c, 0x00, 0xfc, 0x00,
  114.    0xfc, 0x01, 0xec, 0x03, 0xc0, 0x37, 0x80, 0x3f, 0x00, 0x3f, 0x00, 0x3e,
  115.    0x00, 0x3f, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00}},
  116.   {{    // FL_CURSOR_NESW
  117.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x1c, 0x00, 0x1e,
  118.    0x00, 0x17, 0x80, 0x03, 0xc0, 0x01, 0xe8, 0x00, 0x78, 0x00, 0x38, 0x00,
  119.    0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  120.    {
  121.    0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x3f, 0x00, 0x3e, 0x00, 0x3f,
  122.    0x80, 0x3f, 0xc0, 0x37, 0xec, 0x03, 0xfc, 0x01, 0xfc, 0x00, 0x7c, 0x00,
  123.    0xfc, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00}},
  124.   {{0}, {0}} // FL_CURSOR_NONE & unknown
  125. };
  126.  
  127. void Fl_Window::cursor(Fl_Cursor c, Fl_Color fg, Fl_Color bg) {
  128.   if (!shown()) return;
  129.   Cursor cursor;
  130.   int deleteit = 0;
  131.   if (!c) {
  132.     cursor = None;
  133.   } else {
  134.     if (c >= FL_CURSOR_NS) {
  135.       TableEntry *q = (c > FL_CURSOR_NESW) ? table+4 : table+(c-FL_CURSOR_NS);
  136.       if (!(q->cursor)) {
  137.     XColor dummy;
  138.     Pixmap p = XCreateBitmapFromData(fl_display,
  139.       RootWindow(fl_display, fl_screen), (const char*)(q->bits),
  140.       CURSORSIZE, CURSORSIZE);
  141.     Pixmap m = XCreateBitmapFromData(fl_display,
  142.       RootWindow(fl_display, fl_screen), (const char*)(q->mask),
  143.       CURSORSIZE, CURSORSIZE);
  144.     q->cursor = XCreatePixmapCursor(fl_display, p,m,&dummy, &dummy,
  145.                     HOTXY, HOTXY);
  146.     XFreePixmap(fl_display, m);
  147.     XFreePixmap(fl_display, p);
  148.       }
  149.       cursor = q->cursor;
  150.     } else {
  151.       cursor = XCreateFontCursor(fl_display, (c-1)*2);
  152.       deleteit = 1;
  153.     }
  154.     XColor fgc;
  155.     uchar r,g,b;
  156.     Fl::get_color(fg,r,g,b);
  157.     fgc.red = r<<8; fgc.green = g<<8; fgc.blue = b<<8;
  158.     XColor bgc;
  159.     Fl::get_color(bg,r,g,b);
  160.     bgc.red = r<<8; bgc.green = g<<8; bgc.blue = b<<8;
  161.     XRecolorCursor(fl_display, cursor, &fgc, &bgc);
  162.   }
  163.   XDefineCursor(fl_display, fl_xid(this), cursor);
  164.   if (deleteit) XFreeCursor(fl_display, cursor);
  165. }
  166.  
  167. #endif
  168.  
  169. //
  170. // End of "$Id: fl_cursor.cxx,v 1.6 1999/01/07 19:17:37 mike Exp $".
  171. //
  172.